package helper;

import com.google.gson.Gson;

import play.Logger;
import play.libs.Crypto;
/***
 * 
 * @author eliotLiu
 *
 */
public class Cookie {
	public final String ip;
	public final String uuid;
	public final String ts;					// time stamp
	public final String profile;			// isAdmin?
	
	public String toString() {
		return String.format("Cookie: id=%s, ip=%s, profile=%s, ts=%s, name=%s, code=%s",
							uuid, ip, profile, ts);
	}
	
	public Cookie(String ip,String uuid,String ts,String profile)
	{
		this.ip = ip;
		this.ts = ts;
		this.profile = profile;
		this.uuid = uuid;
	}
	
	public String encode() {
		Gson gson = new Gson();
		return Crypto.encryptAES(gson.toJson(this));
	}
	
	public static Cookie decode(String cookieString) {
		Gson gson = new Gson();
		try {
			return gson.fromJson(Crypto.decryptAES(cookieString), Cookie.class);
		} catch (Exception e) {
			return null;
		}
	}
}